import requests
import jsonpath


class Weather:
    def __init__(self, config_dict):
        self.config = config_dict

        res = requests.get("https://api.caiyunapp.com/v2.6/"
                           f"{self.config['token']}/"
                           f"{self.config['dailysteps']},{self.config['longitude']}/"
                           "daily?dailysteps=1")
        self.weather_data = res.json()

        self.tem = self.get_tem()
        self.speed = self.get_speed()
        self.weather_condition = self.get_weather_condition()
        self.visibility = self.get_visibility()
        self.air_quality = self.get_air_quality()

    def get_tem(self) -> tuple:
        # 返回最高最低低温
        temperature = jsonpath.jsonpath(self.weather_data, '$.result.daily.temperature[0]')[0]
        tem_max, tem_min = temperature['max'], temperature['min']
        print(f"最高气温：{tem_max}\n最低气温：{tem_min}")
        return tem_max, tem_min

    def get_speed(self) -> str:
        # 返回当天平均风速
        return jsonpath.jsonpath(self.weather_data, '$.result.daily.wind[0][avg][speed]')[0]

    def get_visibility(self) -> str:
        # 返回当天平均能见度
        return jsonpath.jsonpath(self.weather_data, '$.result.daily.visibility[0][avg]')[0]

    def get_air_quality(self) -> str:

        # 返回空气质量
        air_quality = float(self.weather_data["result"]['daily']['air_quality']['pm25'][0]['max'])
        if air_quality > 250:
            re_air_quality = f"(严重污染) PM2.5浓度：{air_quality} \n                      在家呆着！！！"
        elif air_quality > 150:
            re_air_quality = f"(重度污染)  PM2.5浓度：{air_quality} \n                      不许出门儿喔"
        elif air_quality > 115:
            re_air_quality = f"(中度污染) PM2.5浓度：{air_quality} \n                      咱能不出就别出了吧，一定要出门的话得戴口罩哟"
        elif air_quality > 75:
            re_air_quality = f"(轻度污染)  PM2.5浓度：{air_quality} \n                      出门需要戴口罩噢"
        elif air_quality > 35:
            re_air_quality = f"(良) PM2.5浓度：{air_quality} \n                       空气质量还不戳"
        else:
            re_air_quality = f"(优)  PM2.5浓度：{air_quality} \n                       空气这么好，出来约会呀~"

        return re_air_quality

    def get_weather_condition(self) -> str:
        # 返回天气状态

        weather_chn_dict = {
            "CLEAR_DAY": ["#晴（白天）", "今天天气适合晒太阳哦 ^_^ "],
            "CLEAR_NIGHT": ["#晴（夜间）", "今天天气阳光正暖哦,又是开心的一天~"],
            "PARTLY_CLOUDY_DAY": ["#多云（白天）", "今天多云,可以去锻炼锻炼~"],
            "PARTLY_CLOUDY_NIGHT": ["#多云（夜间）", "多云天气，凉风嗖嗖~"],
            "CLOUDY": ["#阴天", "大阴天也不能影响心情哦~"],
            "LIGHT_HAZE": ["#轻度雾霾", "轻度雾霾,注意戴口罩哦~"],
            "MODERATE_HAZE": ["#中度雾霾", "雾霾有点重,少外出哦~"],
            "HEAVY_HAZE": ["#重度雾霾", "重度霾,还是不要不出啦 -_- ~"],
            "LIGHT_RAIN": ["#小雨", "小雨滴滴答答,心里嘻嘻哈哈~"],
            "MODERATE_RAIN": ["#中雨", "出门一定要记得带好伞哦~"],
            "HEAVY_RAIN": ["#大雨", "这么大的雨,哪里都不要去,哼~"],
            "STORM_RAIN": ["#暴雨", "暴雨就在家躺平看风景趴~"],
            "LIGHT_SNOW": ["#小雪", "有雪来啦,但是要注意保暖哦~"],
            "MODERATE_SNOW": ["#中雪", "这个天气最适合玩雪啦,但是衣服可别忘了加厚哦~~"],
            "HEAVY_SNOW": ["#大雪", "下大雪啦,明天可以出去打雪仗~"],
            "DUST": ["#浮尘", "不多说啦，今天必须戴好口罩哦!"],
            "SAND": ["#沙尘", "沙尘天气,就不要出去当仙人掌咯~"],
            "WIND": ["#大风", "北风呼呼哪里来，你也不要想着往哪里去，穿好衣服哦~"],

        }

        weather_condition = jsonpath.jsonpath(self.weather_data, '$.result.daily.skycon[0][value]')[0]
        re = weather_chn_dict[weather_condition][0] + "  " + weather_chn_dict[weather_condition][1]
        return str(re)
